package com.yzt.minemodule.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.zhuju.arouter.RouterMine;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.dialog.MenuSelectBaseDialog;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.mine.FeedBackBean;
import com.yzt.zhuju.network.entity.mine.FileBean;
import com.yzt.zhuju.upload.UploadFileManager;
import com.yzt.zhuju.utils.DeviceUtils;
import com.yzt.zhuju.utils.ModulePictureSelectUtils;
import com.yzt.zhuju.widget.tuiview.TUITextView;
import com.yzt.minemodule.R;
import com.yzt.minemodule.adapter.FeedBackAdapter;
import com.yzt.p_base.utils.DisplayUtils;
import com.yzt.p_base.utils.ToastUtil;
//import com.wildma.pictureselector.PictureSelector;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.picture.decoration.GridSpacingItemDecoration;

/**
 * author : bob
 * date   : 2022/12/8
 * desc   : 意见反馈
 */
@Deprecated
@SuppressLint({"SetTextI18n", "DefaultLocale"})
@Route(path = RouterMine.ACTIVITY_FEED_BACK)
public class FeedbackActivity extends BaseActivity {

    private List<FeedBackBean> dataList;

    private FeedBackAdapter feedBackAdapter;

    private EditText etContent;

    private TextView tvNumber;

    private TUITextView tvStyle;

    private int positionId = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    public void installViews() {

        mTitleBar.setTitle(R.string.feed_back);

        etContent = findViewById(R.id.et_content);

        dataList = new ArrayList<>();

        dataList.add(new FeedBackBean(FeedBackBean.ADD));

        tvNumber = findViewById(R.id.tv_number);

        tvStyle = findViewById(R.id.tv_style);

        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvNumber.setText(etContent.getText().toString().length() + "/400");
            }
        });

        RecyclerView rvFeedBack = findViewById(R.id.rv_feedback);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        rvFeedBack.setLayoutManager(gridLayoutManager);
        rvFeedBack.addItemDecoration(new GridSpacingItemDecoration(4, DisplayUtils.dp2px(10), false));
        feedBackAdapter = new FeedBackAdapter(dataList);
        rvFeedBack.setAdapter(feedBackAdapter);

        feedBackAdapter.setOnItemClickListener((adapter, view, position) -> {
            int itemViewType = adapter.getItemViewType(position);
            if (itemViewType == FeedBackBean.ADD) {
               // PictureSelector.create(mActivity, PictureSelector.SELECT_REQUEST_CODE).selectPicture(false, DisplayUtils.dp2px(1080), DisplayUtils.dp2px(1080), 1, 1);
            }
        });

        feedBackAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.iv_delete) {
                dataList.remove(position);
                if (dataList.get(0).getItemType() != FeedBackBean.ADD) {
                    dataList.add(new FeedBackBean(FeedBackBean.ADD));
                }
                feedBackAdapter.setNewInstance(dataList);
                feedBackAdapter.notifyDataSetChanged();
            }
        });

        findViewById(R.id.tv_commit).setOnClickListener(v -> {
            String content = etContent.getText().toString();
            if (content.length() == 0) {
                ToastUtil.showMessage(getString(R.string.please_complete_info));
                return;
            }

            StringBuilder sb = new StringBuilder();
            for (FeedBackBean bean : dataList) {
                if (bean.getItemType() != FeedBackBean.ADD) {
                    sb.append(bean.getImageUrl()).append(",");
                }
            }

            commitFeedBack(sb.toString());
        });

        // 类型选择
        tvStyle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> list = new ArrayList<>();
                list.add(getString(R.string.feed_about_chat_room));
                list.add(getString(R.string.fault_problems));
                list.add(getString(R.string.functional_problems));
                list.add(getString(R.string.social_problems));
                list.add(getString(R.string.recharge_problems));
                list.add(getString(R.string.other_problems));
                MenuSelectBaseDialog menuSelectDialog = new MenuSelectBaseDialog(FeedbackActivity.this, R.style.ActionSheetDialogStyle, list);
                menuSelectDialog.setOnItemClickListener(i1 -> {
                    positionId = i1;
                    tvStyle.setText(list.get(positionId));
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null) {

                String picturePath = ModulePictureSelectUtils.getPicturePath(data);

                showMProgress();
                UploadFileManager.getInstance().upload(picturePath,0, new UploadFileManager.UploadCallback() {
                    @Override
                    public void uploadSuccess(FileBean url) {
                        FeedBackBean newFileBean = new FeedBackBean(FeedBackBean.IMAGE, url.getPath(), picturePath);
                        feedBackAdapter.addData(newFileBean);
                        dataList = feedBackAdapter.getData();
                        if (dataList.size() == 6) {
                            dataList = dataList.subList(1, 5);
                        }
                        feedBackAdapter.setNewInstance(dataList);
                        feedBackAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void uploadFail(int code, String info) {

                    }
                });
            }
        }
    }

    /**
     * 提交意见反馈
     */
    private void commitFeedBack(String imagePath) {

        String style = tvStyle.getText().toString();
        String content = etContent.getText().toString();
        String model = DeviceUtils.getDeviceBrand() + ":" + DeviceUtils.getSysVersion();
        BizLayer.getInstance().getChatRoomModule().postUploadChatRoomFeedBack(style, content, imagePath, model, new AresSubscriber<Object>() {
            @Override
            protected void onSuccess(Object mObject) {
                ToastUtil.showMessage(getString(R.string.submit_success));
                mActivity.finish();
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                closeProgress();
            }
        });
    }
}
